define(function (require, exports, module) {


    //  获取加息券标识数据
    $.ajax({
        url: '/app/uprate/initUseable.html?randomTime=' + (new Date()).getTime(),
        type: 'post',
        dataType: 'json',
        success: function (data) {
            $("#canUseCouponMoney").val(data.count);
            if (data.count == 0) {
                $(".choiceCoupon").addClass("hide");
                $(".choiceCoupon").after("<div class=\"qd-clear\"></div><span>当前没有可使用加息券</span><div class=\"qd-clear\"></div>");
            }
        }
    });

        var lowestAmount = $("#lowestAccount").text(),//最小投标金额
        mostAmount = $("#mostAccount").text(),//最大投标金额
        accountWait = $("#accountWait").text();//剩余可投金额
    //***3：加息券
    var $choiceCoupon = $('.choiceCoupon'),//选择加息券按钮
        $couponRadio = $('.js_couponRadio');//单个加息券选择
    //	选择加息券弹窗
    $choiceCoupon.click(function () {
        var $userMoney = $money.val();
        if ($userMoney == '' || $userMoney == 0) {
            $(".form_mony_tip").removeClass("hide").html('<b class="iconfont"><!--&#xe60f;-->*</b>请先输入投标金额！');
            return
        }
        if ($userMoney < parseInt(lowestAmount) && parseInt(accountWait) > parseInt(lowestAmount)) {
            $(".form_mony_tip").removeClass("hide").html('投标金额不能小于最小投标金额<a href="javascript:;" id="invest_all">' + lowestAmount + '</a>元！');
        }
        else {
            $.ajax({
                url: '/app/uprate/useable.html?randomTime=' + (new Date()).getTime(),
                type: 'get',
                dataType: 'json',
                cache: false,
                success: function (data) {

                    var jdata = data;

                    if (!jdata.rows.length)//传过来得json数据为空即没有可用的加息券
                    {
                        $('.choiceCoupon').html('<em>您没有可用的加息券！</em>');
                    }
                    else {
                        if ($(".formCou").html() == '' || $(".formCou").html() == 0) {
                            $("#upMoneyVal").text(0);
                        } else {
                            $(".choiceCouponMoney").html(0);

                        }

                        //	选择加息券
                        var $couponIdLi, $couponId, $this, $couponValueEm, $couponBtn, $choiceCouponMoney;
                        require.async(['/plugins/layer-v1.8.4/skin/layer.css'], function () {
                            window.layer.open({
                                type: 1,
                                closeBtn: [0, true],
                                title: "请选择可使用的加息券(仅限使用一张哦!)",
                                area: ['300px', 'auto'],
                                border: [1, 1, '#cecfd0'],
                                shift:3,
                                content: $('#choiceCoupon'),
                                success: function () {

                                    $("#couponList").html(Handlebars.compile(require("../../../../tpl/invest/ext/couponList.tpl"))(jdata));
                                    $('.choiceCouponMoney').text($('.formCou').text());
                                    var dataId = $('#rateInterestId').val();

                                    $('#id' + dataId).prop('checked', true);
                                    $('#id' + dataId).parent().addClass('radioBg2');
                                    $couponValueEm = $('#id' + dataId).parent().parent().find('.couponValueEm').text();
                                    conponFn($('.js_couponLi'));
                                    $('.couponConfirm').on('click', function () {
                                        $('#rateInterestId').val($('.myRIRCouponId').val());
                                        layer.closeAll();
                                    });

                                },
                                close: function (index) {

                                    $(".couponMoney").text($("#upMoneyVal").text());
                                    layer.closeAll();
                                }
                            });
                        })
                    }
                }
            })
        }
    })
    //	加息券选择功能
    function conponFn(obj) {
        obj.on('click', function () {
            $this = $(this);
            $couponBtn = $this.find('.radioBg');
            $choiceCouponMoney = $('.choiceCouponMoney');
            $couponIdLi = $this.find('.js_couponRadio');
            $couponId = $('.couponConfirm').data('val');
            $couponId = $couponIdLi.val();
            if ($couponBtn.hasClass('radioBg2')) {
                $couponBtn.removeClass('radioBg2');
                $couponIdLi.prop('checked', false);
                $choiceCouponMoney.html(0);
                $('.couponMoney').html(0);
                $couponIdLi.val('');
                $couponIdLi.attr('id', 'id');
                $('.couponConfirm').on('click', function () {
                    $('.myRIRCouponId').val($couponIdLi.val());
                    $('#rateInterestId').val($couponIdLi.val());
                    $('.choiceCoupon').html('使用加息券：<em class="formCou">0</em>%');
                    layer.closeAll();
                    $("#upMoneyVal").text(0);
                    $(".couponMoney").text($("#upMoneyVal").text());
                });

            }
            else {
                $couponBtn.addClass('radioBg2');
                $couponIdLi.prop('checked', true);
                $this.siblings().find('.radioBg').removeClass('radioBg2');
                $this.siblings().find('.js_couponRadio').prop('checked', false);
                $couponValueEm = $this.find('.couponValueEm').html();
                var
                    upaprVal = $couponValueEm,
                    aprVal = $("#aprVal").text(),
                    limitVal = $("#limitVal").text(),
                    moneyVal = $("#money").val(),
                    styleVal = $("#styleVal").text(),
                    isDayVal = $("#isDayVal").text();

                $.ajax({
                    type: "post",
                    url: "/app/public/upRateInterestJson.html?randomTime=" + (new Date()).getTime(),
                    data: {
                        upapr: upaprVal,
                        apr: aprVal,
                        limit: limitVal,
                        money: moneyVal,
                        style: styleVal,
                        isDay: isDayVal
                    },
                    dataType: "json",
                    cache: false,
                    success: function (data) {

                        $("#upMoneyVal").text(data.upInterest);
                        $(".couponMoney").text($("#upMoneyVal").text());
                        $choiceCouponMoney.html($couponValueEm);
                        var $id = $couponIdLi.val();

                        $('.couponConfirm').on('click', function () {
                            $('.myRIRCouponId').val($id);
                            $('#rateInterestId').val($id);
                            $('.choiceCoupon').html('使用加息券：<em class="formCou">' + $couponValueEm + '</em>%');
                            layer.closeAll();
                        });
                    },
                    error: function (data) {
                    }

                });
                $("#upMoneyVal").text();
            }
        });
    }


});